package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.core.dao.database.IPhoneSecurityCodeDao;
import com.singfan.core.dao.database.model.PhoneSecurityCode;
import org.springframework.stereotype.Repository;

/**
 * Author: hansonghe
 * Date: 2015-08-21 14:01
 * Desc:
 */
@Repository
public class PhoneSecurityCodeDaoImpl extends DaoBaseGeneric<PhoneSecurityCode> implements IPhoneSecurityCodeDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void createPhoneSecurityCode(String phoneNumber, String securityCode) {
        PhoneSecurityCode phoneSecurityCode = new PhoneSecurityCode();
        phoneSecurityCode.setPhoneNumber(phoneNumber);
        phoneSecurityCode.setSecurityCode(securityCode);
        phoneSecurityCode.setCreateTime(System.currentTimeMillis());

        insertWithAutoIncrement(null, phoneSecurityCode);
    }

    @Override
    public PhoneSecurityCode findPhoneSecurityCode(String phoneNumber, String securityCode) {
        String sql = "SELECT * FROM " + getTableName(phoneNumber) + " WHERE phone_number=? AND security_code=?";
        return selectOne(phoneNumber, sql, phoneNumber, securityCode);
    }

    @Override
    public void deleteOne(String phoneNumber, String securityCode) {
        String sql = "DELETE FROM " + getTableName(phoneNumber) + " WHERE phone_number=? AND security_code=?";
        execute(phoneNumber, sql, phoneNumber, securityCode);
    }
}
